<template>
  <el-container class="homeheader">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 左边区域 -->
      <el-aside :width="iscollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="togget">| |</div>
        <!-- 用户管理 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="iscollapse"
          :collapse-transition="false"
          router
          :default-active="activepah"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in bookslist"
            v-bind:key="item.id"
          >
            <template slot="title">
              <i :class="icons[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subitem.path"
              v-for="subitem in item.children"
              v-bind:key="subitem.id"
              @click="active('/' + subitem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subitem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右边主体区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data: function() {
    return {
      activepah: '',
      // 是否折叠
      iscollapse: false,
      bookslist: [],
      icons: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      }
    }
  },
  // 生命周期  界面一加载就获取菜单数据
  created() {
    this.getData()
    this.activepah = window.sessionStorage.getItem('active')
  },
  methods: {
    logout: function() {
      // 删除token
      window.sessionStorage.clear('taoken')
      // 跳转到login
      this.$router.push('/login')
    },
    getData: async function() {
      // 获取数据
      const { data: res } = await this.$http.get('menus')
      // console.log(res.data.data)
      this.bookslist = res.data
    },
    togget: function() {
      this.iscollapse = !this.iscollapse
    },
    active: function(active) {
      this.activepah = active
      window.sessionStorage.setItem('active', active)
    }
  }
}
</script>
<style lang="less" scoped>
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  div {
    display: flex;
    align-items: center;
    img {
      width: 50px;
      height: 50px;
      border: 1px solid #ddd;
      border-radius: 50%;
    }
    span {
      color: #fff;
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.homeheader {
  height: 100%;
}
.el-submenu i {
  margin-right: 6px;
}
.el-menu {
  border-right: none;
}
.toggle-button {
  color: #fff;
  text-align: center;
  font-size: 10px;
  line-height: 24px;
  cursor: pointer;
}
</style>
